//
//  LoginView.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/14.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "LoginView.h"
typedef NS_ENUM(NSInteger,ClickType){
    clicktypeNone,
    clicktypeUser,
    clicktypePass
};
@interface LoginView()<UITextFieldDelegate>
//猫的手
@property (strong,nonatomic) UIImageView *lefthand;//左手
@property (strong,nonatomic) UIImageView *righthand;//右手


//猫的蒙眼胳膊
@property (strong,nonatomic) UIImageView *lefthArm;//左胳膊
@property (strong,nonatomic) UIImageView *rightArm;//右胳膊
@property (assign,nonatomic) ClickType clicktype;

@end
@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self allViews];
    }
    return self;
}
- (void)allViews
{
    //导航标题
    self.title = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - (40 * LAYOUT_SIZE), 29 * LAYOUT_SIZE_HEIGHT, 80* LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];
    self.title.text = @"登陆";
    self.title.textAlignment = 1;
    self.title.textColor = [UIColor yellowColor];
    self.title.font = [UIFont systemFontOfSize:17 * LAYOUT_SIZE];
    [self addSubview:self.title];
    
    //取消按钮
    self.cancleButton = [[UIButton alloc]initWithFrame:CGRectMake(10 * LAYOUT_SIZE, 29 * LAYOUT_SIZE_HEIGHT, 50 * LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];
    [self.cancleButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [self addSubview:self.cancleButton];

    //添加登陆界面
    [self pushLgoinView];
}
- (void)pushLgoinView
{
    //背景图片
    self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 320)/2, 150 * LAYOUT_SIZE_HEIGHT, 320 * LAYOUT_SIZE, 350 * LAYOUT_SIZE_HEIGHT)];
    self.backgroundView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.backgroundView];
    //猫头鹰的脑袋图片
    UIImageView *head = [[UIImageView alloc]initWithFrame:CGRectMake(85 * LAYOUT_SIZE, 27 * LAYOUT_SIZE_HEIGHT, 150 * LAYOUT_SIZE, 80 * LAYOUT_SIZE_HEIGHT)];
    head.image = [UIImage imageNamed:@"header"];
    [self.backgroundView addSubview:head];
    //左胳膊
    _lefthArm = [[UIImageView alloc]initWithFrame:CGRectMake(75 * LAYOUT_SIZE, 100 * LAYOUT_SIZE_HEIGHT, 40 * LAYOUT_SIZE, 65 * LAYOUT_SIZE_HEIGHT)];
    _lefthArm.image = [UIImage imageNamed:@"left"];
    [self.backgroundView addSubview:_lefthArm];
    //右胳膊
    _rightArm = [[UIImageView alloc]initWithFrame:CGRectMake(215 * LAYOUT_SIZE, 100 * LAYOUT_SIZE_HEIGHT, 40 * LAYOUT_SIZE, 65 * LAYOUT_SIZE_HEIGHT)];
    _rightArm.image = [UIImage imageNamed:@"right"];
    [self.backgroundView addSubview:_rightArm];
    //登陆界面
    self.loginPresentView = [[UIView alloc]initWithFrame:CGRectMake(0, 100 * LAYOUT_SIZE_HEIGHT, 320 * LAYOUT_SIZE, 250 * LAYOUT_SIZE_HEIGHT)];
    self.loginPresentView.backgroundColor = ColorHex(0x0d1c38);
    self.loginPresentView.layer.cornerRadius = 8;
    self.loginPresentView.layer.masksToBounds = YES;
    [self.backgroundView addSubview:self.loginPresentView];
    //左手
    _lefthand = [[UIImageView alloc]initWithFrame:CGRectMake(75 * LAYOUT_SIZE, 83 * LAYOUT_SIZE_HEIGHT, 35 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    _lefthand.image = [UIImage imageNamed:@"hand"];
    [self.backgroundView addSubview:_lefthand];
    //右手
    _righthand = [[UIImageView alloc]initWithFrame:CGRectMake(210 * LAYOUT_SIZE, 83 * LAYOUT_SIZE_HEIGHT, 35 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    _righthand.image = [UIImage imageNamed:@"hand"];
    [self.backgroundView addSubview:_righthand];
    
    //登陆背景图片
    self.loginPresentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50 * LAYOUT_SIZE, 10 * LAYOUT_SIZE_HEIGHT, 220 * LAYOUT_SIZE, 25 * LAYOUT_SIZE_HEIGHT)];
    self.loginPresentImageView.image = [UIImage imageNamed:@"logo1"];
    [self.loginPresentView addSubview:self.loginPresentImageView];
    //账号背景图片
    self.userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20 * LAYOUT_SIZE, 65 * LAYOUT_SIZE_HEIGHT, 30 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    self.userImageView.image = [UIImage imageNamed:@"phoneNumberBackGround@2x"];
    [self.loginPresentView addSubview:self.userImageView];
    //小人儿图片
    self.peopleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(25 * LAYOUT_SIZE, 70 * LAYOUT_SIZE_HEIGHT, 20 * LAYOUT_SIZE, 20 * LAYOUT_SIZE_HEIGHT)];
    self.peopleImageView.image = [UIImage imageNamed:@"iconfont-ren1"];
    [self.loginPresentView addSubview:self.peopleImageView];
    //账号背景图片
    self.userBackImageView = [[UIImageView alloc]initWithFrame:CGRectMake(55 * LAYOUT_SIZE, 65 * LAYOUT_SIZE_HEIGHT, 245 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    self.userBackImageView.image = [UIImage imageNamed:@"phoneNumberBackGroundInput@2x"];
    [self.loginPresentView addSubview:self.userBackImageView];
    //账号输入框
    self.usernameTF = [[UITextField alloc]initWithFrame:CGRectMake(60 * LAYOUT_SIZE, 65 * LAYOUT_SIZE_HEIGHT, 230 * LAYOUT_SIZE , 30 * LAYOUT_SIZE_HEIGHT)];
    self.usernameTF.placeholder = @"账号";
    self.usernameTF.delegate = self;
    self.usernameTF.tag = 10010;
    self.usernameTF.clearButtonMode = UITextFieldViewModeAlways;
    [self.usernameTF setValue:[UIFont boldSystemFontOfSize:16 * LAYOUT_SIZE] forKeyPath:@"_placeholderLabel.font"];
    [self.usernameTF setValue:ColorHexA(0xFFFFE0, 0.7) forKeyPath:@"_placeholderLabel.textColor"];
    [self.loginPresentView addSubview:self.usernameTF];
    
    //密码背景图片
    self.passImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20 * LAYOUT_SIZE, 105 * LAYOUT_SIZE_HEIGHT, 30 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    self.passImageView.image = [UIImage imageNamed:@"phoneNumberBackGround@2x"];
    [self.loginPresentView addSubview:self.passImageView];
    //钥匙图片
    self.keyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(25 * LAYOUT_SIZE, 110 * LAYOUT_SIZE_HEIGHT, 20 * LAYOUT_SIZE, 20 * LAYOUT_SIZE_HEIGHT)];
    self.keyImageView.image = [UIImage imageNamed:@"iconfont-michi"];
    [self.loginPresentView addSubview:self.keyImageView];
    //密码背景图片
    self.passBackImageView = [[UIImageView alloc]initWithFrame:CGRectMake(55 * LAYOUT_SIZE, 105 * LAYOUT_SIZE_HEIGHT, 245 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    self.passBackImageView.image = [UIImage imageNamed:@"phoneNumberBackGroundInput@2x"];
    [self.loginPresentView addSubview:self.passBackImageView];
    //密码输入框
    self.passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(60 * LAYOUT_SIZE, 105 * LAYOUT_SIZE_HEIGHT, 230 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    self.passwordTF.placeholder = @"密码";
    self.passwordTF.delegate = self;
    [self.passwordTF setValue:[UIFont systemFontOfSize:16 * LAYOUT_SIZE] forKeyPath:@"_placeholderLabel.font"];
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.tag = 10011;
    self.passwordTF.clearButtonMode = UITextFieldViewModeAlways;
    [self.passwordTF setValue:ColorHexA(0xFFFFE0, 0.7) forKeyPath:@"_placeholderLabel.textColor"];
    [self.loginPresentView addSubview:self.passwordTF];
    //找回密码按钮
    self.findPWDButton = [[UIButton alloc]initWithFrame:CGRectMake(230 * LAYOUT_SIZE, 145 * LAYOUT_SIZE_HEIGHT, 50 * LAYOUT_SIZE, 10 * LAYOUT_SIZE_HEIGHT)];
    [self.findPWDButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    self.findPWDButton.titleLabel.font = [UIFont systemFontOfSize:10 * LAYOUT_SIZE];
    [self.findPWDButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [self.loginPresentView addSubview:self.findPWDButton];
    //注册按钮
    self.registButton = [[UIButton alloc]initWithFrame:CGRectMake(20 * LAYOUT_SIZE, 170 * LAYOUT_SIZE_HEIGHT, 135 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    [self.registButton setTitle:@"注册" forState:UIControlStateNormal];
    self.registButton.backgroundColor = ColorHex(0x90EE90);
    self.registButton.layer.masksToBounds = YES;
    self.registButton.layer.cornerRadius = 8;
    [self.loginPresentView addSubview:self.registButton];
    //登陆按钮
    self.loginButton = [[UIButton alloc]initWithFrame:CGRectMake(165 * LAYOUT_SIZE, 170 * LAYOUT_SIZE_HEIGHT, 135 * LAYOUT_SIZE, 30 * LAYOUT_SIZE_HEIGHT)];
    [self.loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    self.loginButton.backgroundColor = ColorHex(0x90EE90);
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.layer.cornerRadius = 8;
    [self.loginPresentView addSubview:self.loginButton];
    
}
#pragma mark - 监控键盘方法(控制猫头鹰手臂)
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([textField isEqual:_usernameTF]) {
        if (_clicktype != clicktypePass) {
            _clicktype =clicktypeUser;
            return;
        }
        _clicktype=clicktypeUser;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.lefthArm.frame = CGRectMake(self.lefthArm.frame.origin.x - 45 * LAYOUT_SIZE, self.lefthArm.frame.origin.y + 30 * LAYOUT_SIZE_HEIGHT, self.lefthArm.frame.size.width, self.lefthArm.frame.size.height);
            
            self.rightArm.frame = CGRectMake(self.rightArm.frame.origin.x+48 * LAYOUT_SIZE, self.rightArm.frame.origin.y + 30 * LAYOUT_SIZE_HEIGHT, self.rightArm.frame.size.width, self.rightArm.frame.size.height);
            
            self.lefthand.frame = CGRectMake(self.lefthand.frame.origin.x-70 * LAYOUT_SIZE, self.lefthand.frame.origin.y, 40 * LAYOUT_SIZE, 40 * LAYOUT_SIZE_HEIGHT);
            self.righthand.frame = CGRectMake(self.righthand.frame.origin.x +30 * LAYOUT_SIZE, self.righthand.frame.origin.y, 40 * LAYOUT_SIZE, 40 * LAYOUT_SIZE_HEIGHT);
        } completion:^(BOOL finished) {
            
        }];
    }else if ([textField isEqual:_passwordTF]){
        if (_clicktype == clicktypePass)
        {
            return;
        }
        _clicktype = clicktypePass;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.lefthArm.frame = CGRectMake(self.lefthArm.frame.origin.x + 45 * LAYOUT_SIZE, self.lefthArm.frame.origin.y - 30 * LAYOUT_SIZE_HEIGHT, self.lefthArm.frame.size.width, self.lefthArm.frame.size.height);
            
            self.rightArm.frame = CGRectMake(self.rightArm.frame.origin.x - 48 * LAYOUT_SIZE, self.rightArm.frame.origin.y - 30 * LAYOUT_SIZE_HEIGHT, self.rightArm.frame.size.width, self.rightArm.frame.size.height);
            
            self.lefthand.frame = CGRectMake(self.lefthand.frame.origin.x + 70 * LAYOUT_SIZE, self.lefthand.frame.origin.y, 0, 0);
            self.righthand.frame = CGRectMake(self.righthand.frame.origin.x - 30 * LAYOUT_SIZE, self.righthand.frame.origin.y, 0, 0);
        } completion:^(BOOL finished) {
            
        }];
    }
    
}

@end
